package com.zbs.framework.core.user.action;

import com.zbs.framework.common.utils.ErrorAssert;
import com.zbs.framework.common.utils.ResultData;
import com.zbs.framework.common.utils.ResultUtil;
import com.zbs.framework.dao.user.UserConst;
import com.zbs.framework.dao.user.entity.BaseUserInfo;
import com.zbs.framework.dao.user.params.UserParam;
import com.zbs.framework.dao.user.service.BaseUserService;
import com.zbs.framework.dao.user.view.BaseUserInfoView;
import com.zbs.framework.security.utils.ThreadLocalClient;
import com.zbs.framework.swagger.annotations.ApiOperation;
import com.zbs.framework.swagger.annotations.ApiParam;
import com.zbs.framework.swagger.annotations.ApiTags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@ApiTags(value = "用户中心", group = "UIM")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private BaseUserService baseUserService;

    /**
     * 用户注册
     * @param username
     * @param password
     * @return
     */
    @ApiOperation("用户名注册")
    @PostMapping("/registerNormal.do")
    public ResultData<?> registerNormal(@ApiParam("用户名") String username,@ApiParam("密码") String password) {
        baseUserService.registerNormal(username, password);
        return ResultUtil.ok();
    }


    @ApiOperation("用户名登录")
    @PostMapping("/usernameLogin.do")
    public ResultData<Object> usernameLogin(@ApiParam(value = "用户名",required = true) String username,@ApiParam(value = "密码",required = true) String password,@ApiParam("登录入口类型 1 web 2 app 3 公众号") String entryType) {
        Object o = baseUserService.usernameLogin(username, password, entryType);
        return ResultUtil.ok(o);
    }

    @ApiOperation("手机号登录")
    @PostMapping("/phoneLogin.do")
    public ResultData<Object> phoneLogin(@ApiParam("手机号") String phone,@ApiParam("验证码") String code,@ApiParam("登录入口类型 1 web 2 app 3 公众号") String entryType) {
        Object o = baseUserService.phoneLogin(phone, code, entryType);
        return ResultUtil.ok(o);
    }

    /**
     * 获取等去登录用户信息
     * @return
     */
    @ApiOperation(value = "获取等去登录用户信息",response = BaseUserInfoView.class)
    @PostMapping("/getMyUserInfo")
    public ResultData<BaseUserInfo> getMyUserInfo() {
        Long userId = ThreadLocalClient.getUserId();
        BaseUserInfo userInfo = this.baseUserService.getMyUserInfo(userId);
        return ResultUtil.ok(userInfo);
    }

    /**
     * 用户注册
     *
     * @param phone
     * @param password
     * @param code
     * @return
     */
    @ApiOperation("用户手机号注册")
    @PostMapping("/registerPhone.do")
    public ResultData<?> registerPhone(@ApiParam("手机号") String phone,
                                    @ApiParam("密码") String password,
                                    @ApiParam("验证码") String code) {
        baseUserService.registerPhone(phone, password, code);
        return ResultUtil.ok();
    }

    /**
     * 修改用户基本信息
     * @param userParam
     * @return
     */
    @ApiOperation("修改当前登录用户基本信息")
    @PostMapping("/changeUserInfo")
    public ResultData<?> changeUserInfo(@RequestBody UserParam userParam) {
        Long userId = ThreadLocalClient.getUserId();
        Integer gender = userParam.getGender();
        String name = userParam.getName();
        String birthday = userParam.getBirthday();
        String headImg = userParam.getHeadImg();
        Character g = null;
        if (gender != null) {
            ErrorAssert.isTrue(gender.equals(1) || gender.equals(0),
                    "性别格式错误!");
            g =  gender == 1? UserConst.MALE:UserConst.FEMALE;
        }
        baseUserService.changeUserInfo(userId,name,headImg,g,birthday);
        return ResultUtil.ok();
    }


    /**
     * 修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @ApiOperation("修改当前登录用户密码")
    @PostMapping("/changePwd")
    public ResultData<?> changePwd(@ApiParam("旧密码") String oldPwd,@ApiParam("新密码") String newPwd) {
        Long userId = ThreadLocalClient.getUserId();
        baseUserService.changePwd(userId, oldPwd, newPwd);
        return ResultUtil.ok();
    }

}
